<?php

class File extends Element
{
	var $random_name = false;
	var $upload      = true;
	var $path_original;
	var $debug = false;
	var $thumb;
	var $type = 'file';
	var $input = "<input type=\"file\" name=\"{name}\" size=\"{size}\" id=\"{id}\"{params} />\n";
	var $typed = false;

	function File($id)
	{
		parent::Element($id);
	}

	function type($type, $debug = false)
	{
		$this->typed = true;
		$this->type = $type;

		$this->__type($type);
	}

	function __type()
	{
		if ($this->type == 'image')
		{
        	require_once(BASE_FRAMEWORK."/Thumb.class.php");
        	$this->thumb = new Thumb($debug);
		} else {
        	require_once(BASE_FRAMEWORK . '/FileUpload.class.php');
			$this->upload = new FileUpload($this->getName(), $this->path_original);
		}
	}

	function __getPostBackValue()
	{
		return $_FILES[$this->getName()];
	}

	function addThumb($thumb)
	{
		if ($this->type == 'image')
		{
			if (!$this->typed)
			{
				$this->__type();
			}
			$this->thumb->setThumb($thumb);
		}
	}

	function __getHTML()
	{
		if ($this->isPostBack)
		{
			if (!$this->typed)
			{
				$this->__type();
			}
            if ($this->default['name'] != '')
            {
	        	if ($this->type == 'image')
				{
					$this->thumb->path_original = $this->path_original;
					$this->thumb->random_name   = $this->random_name;
					$this->thumb->debug         = $this->debug;

	            	$fotito = $this->thumb->Crear($this->default);
	                if ($fotito['error'] != '')
	                {
	                	Debug::Mostrar($fotito['error']);
	                    exit();
	                }

	                $this->__setValue($fotito['nombre_imagen']);
	            } else {
	            	$salida = $this->upload->Upload();

	                $this->__setValue($salida[$this->getName()]);
				}
			}
		}
		$search  = array('{name}', '{size}', '{id}', '{params}');
		$replace = array($this->getName(), $this->size, $this->ID, $this->getParams());

		return str_replace($search, $replace, $this->input);
	}

	function __setValue($value)
	{
		$this->VALUE = $value;
		$_POST[$this->getName()] = $value;
	}
}

?>